/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DataAccessObject;

import EntityObject.tblQuanTriEO;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author minh
 */
public class tblQuanTriDAO {
    public static boolean QuanTri_CheckExists(tblQuanTriEO _tblQuanTriEO){
        try {
            String sql="{call tblQuanTri_CheckExists(?)}";
            CallableStatement cstmt=Connection.conn().prepareCall(sql);
            cstmt.setString(1, _tblQuanTriEO.getPK_sTenDN());
            ResultSet rs=cstmt.executeQuery();
            while(rs.next()) {
                System.out.println(rs.getString("TenLoaiDC"));
            }
            Connection.conn().close();
            return true;
        }
        catch (SQLException e) {
            System.out.println(e);
            return false;
        }
    }
    
    public static boolean QuanTri_Insert(tblQuanTriEO _tblQuanTriEO) throws SQLException{
        try {
            String sql="{call tblQuanTri_Insert(?,?)}";
            CallableStatement cstmt=Connection.conn().prepareCall(sql);
            cstmt.setString(1, _tblQuanTriEO.getPK_sTenDN());
            cstmt.setString(2, _tblQuanTriEO.getsMatKhau());
            cstmt.execute();
            return true;
        }
        catch (SQLException e) {
            System.out.println(e);
            return false;
        }
        finally{Connection.conn().close();}
    }
    
    public static boolean QuanTri_Update(tblQuanTriEO _tblQuanTriEO) throws SQLException{
        try {
            String sql="{call tblQuanTri_Update(?,?)}";
            CallableStatement cstmt=Connection.conn().prepareCall(sql);
            cstmt.setString(1, _tblQuanTriEO.getPK_sTenDN());
            cstmt.setString(2, _tblQuanTriEO.getsMatKhau());
            cstmt.execute();
            return true;
        }
        catch (SQLException e) {
            System.out.println(e);
            return false;
        }
        finally{Connection.conn().close();}
    }
    
    public static boolean QuanTri_Delete(tblQuanTriEO _tblQuanTriEO) throws SQLException{
        try {
            String sql="{call tblQuanTri_Delete(?)}";
            CallableStatement cstmt=Connection.conn().prepareCall(sql);
            cstmt.setString(1, _tblQuanTriEO.getPK_sTenDN());
            cstmt.execute();
            return true;
        }
        catch (SQLException e) {
            System.out.println(e);
            return false;
        }
        finally{Connection.conn().close();}
    }
    
    public static ResultSet QuanTri_SelectList(tblQuanTriEO _tblQuanTriEO) throws SQLException{
        ResultSet rs =null;
        try {
            String sql="{call tblQuanTri_SelectList()}";
            CallableStatement cstmt=Connection.conn().prepareCall(sql);
//            cstmt.setString(1, dungcu.getMaDungCu());
            rs=cstmt.executeQuery();
            return rs;
        }
        catch (SQLException e) {
            System.out.println(e);
            return rs;
        }
        finally{Connection.conn().close();}
    }
    
    public static ResultSet QuanTri_SelectItem(tblQuanTriEO _tblQuanTriEO) throws SQLException{
        ResultSet rs =null;
        try {
            String sql="{call tblQuanTri_SelectItem(?)}";
            CallableStatement cstmt=Connection.conn().prepareCall(sql);
            cstmt.setString(1, _tblQuanTriEO.getPK_sTenDN());
            rs=cstmt.executeQuery();
            return rs;
        }
        catch (SQLException e) {
            System.out.println(e);
            return rs;
        }
        finally{Connection.conn().close();}
    }
    
    public static boolean QuanTri_Login(tblQuanTriEO _tblQuanTriEO) throws SQLException{
        try {
            String sql="{call tblQuanTri_Login(?,?)}";
            CallableStatement cstmt=Connection.conn().prepareCall(sql);
            cstmt.setString(1, _tblQuanTriEO.getPK_sTenDN());
            cstmt.setString(2, _tblQuanTriEO.getsMatKhau());
            ResultSet rs=cstmt.executeQuery();
            while(rs.next()) {
                System.out.println(rs.getString("return_value"));
                return rs.getBoolean("return_value");
            }
            return false;
        }
        catch (SQLException e) {
            System.out.println(e);
            return false;
        }
        finally{Connection.conn().close();}
    }
}
